<?php
/*
	服务公司：沧州讯呗网络科技有限公司
	作者：sun
*/
namespace App\Http\Controllers\Web;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\ProductRepository;
use App\Repositories\CartRepository;
use App\Repositories\DapeiRepository;
use App\Traits\BaseFun;
use App\Repositories\DapeiCartRepository;

class ProductController extends Controller
{
    public function __construct(ProductRepository $ProductRepository,CartRepository $CartRepository,DapeiRepository $DapeiRepository,DapeiCartRepository $DapeiCartRepository)
    {
        $this->ProductRepository=$ProductRepository;
        $this->CartRepository=$CartRepository;
        $this->DapeiRepository=$DapeiRepository;
		$this->DapeiCartRepository=$DapeiCartRepository;
    }
    public function Product(Request $request,$id)
    {   
        if ($id) {
            $res = $this->ProductRepository->list(['category'=>$id,'state'=>1]);
            foreach ($res as $key => $value) {
                $info=$this->CartRepository->info(['proid'=>$value['id'],'userid'=>$request->userId]);
                if ($info) {
                   $res[$key]['cartNum']=$info['num'];
                }else{
                   $res[$key]['cartNum']=0;
                }
                
            }
        }else{
            $res = $this->ProductRepository->list(['is_tj'=>1,'state'=>1]);
            foreach ($res as $key => $value) {
                $info=$this->CartRepository->info(['proid'=>$value['id'],'userid'=>$request->userId]);
                if ($info) {
                   $res[$key]['cartNum']=$info['num'];
                }else{
                   $res[$key]['cartNum']=0;
                }
            }
        }
        
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function ProductInfo(Request $request,$id)
    {
        
        $res = $this->ProductRepository->info(['id'=>$id]);
        $res['cartNum']=$this->CartRepository->info(['proid'=>$res['id'],'userid'=>$request->userId])['num'];
        
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function Dapei(Request $request)
    {
        $res = $this->DapeiRepository->list(['state'=>1]);
        
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
	public function ProductDapei(Request $request)
	{	
		$res = $this->ProductRepository->list(['zixuan'=>1,'state'=>1]);
		foreach ($res as $key => $value) {
			
		    $info=$this->DapeiCartRepository->info(['proid'=>$value['id'],'userid'=>$request->userId]);
			
		    if ($info) {
		       $res[$key]['cartNum']=$info['num'];
		    }else{
		       $res[$key]['cartNum']=0;
		    }
		}
	    if ($res) {
	       $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
		    return BaseFun::jsonReturn($ArrDate);
	    }else{
	       $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
		    return BaseFun::jsonReturn($ArrDate);
	    }
	   
	}
}
